package com.order.api.interceptor;

import com.order.api.constant.BaseException;
import com.order.api.constant.ResultValueEnum;
import com.order.api.vo.BaseResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常拦截器
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(BaseException.class)
    public BaseResult<String> baseException(BaseException ex){
        String msg=ex.getMessage();
        ResultValueEnum resultValueEnum=(ResultValueEnum) ex.getBizExceptionEnum();
        if (resultValueEnum==null){
            resultValueEnum=ResultValueEnum.SYS_ERROR;
        }
        BaseResult<String> result = BaseResult.newInstance(resultValueEnum);
        if (msg!=null){
            result.setMsg(msg);
        }
        return result;
    }
}
